1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.base.Preconditions;
21
22 import javax.annotation.Nullable;
23
24
25
26
27
28
29 @GwtCompatible(serializable = true, emulated = true)
30 @SuppressWarnings("serial")
31 class RegularImmutableList<E> extends ImmutableList<E> {
32 private final transient int offset;
33 private final transient int size;
34 private final transient Object[] array;
35
36 RegularImmutableList(Object[] array, int offset, int size) {
37 this.offset = offset;
38 this.size = size;
39 this.array = array;
40 }
41
42 RegularImmutableList(Object[] array) {
43 this(array, 0, array.length);
44 }
45
46 @Override
47 public int size() {
48 return size;
49 }
50
51 @Override boolean isPartialView() {
52 return size != array.length;
53 }
54
55 @Override
56 int copyIntoArray(Object[] dst, int dstOff) {
57 System.arraycopy(array, offset, dst, dstOff, size);
58 return dstOff + size;
59 }
60
61
62 @Override
63 @SuppressWarnings("unchecked")
64 public E get(int index) {
65 Preconditions.checkElementIndex(index, size);
66 return (E) array[index + offset];
67 }
68
69 @Override
70 public int indexOf(@Nullable Object object) {
71 if (object == null) {
72 return -1;
73 }
74 for (int i = 0; i < size; i++) {
75 if (array[offset + i].equals(object)) {
76 return i;
77 }
78 }
79 return -1;
80 }
81
82 @Override
83 public int lastIndexOf(@Nullable Object object) {
84 if (object == null) {
85 return -1;
86 }
87 for (int i = size - 1; i >= 0; i--) {
88 if (array[offset + i].equals(object)) {
89 return i;
90 }
91 }
92 return -1;
93 }
94
95 @Override
96 ImmutableList<E> subListUnchecked(int fromIndex, int toIndex) {
97 return new RegularImmutableList<E>(
98 array, offset + fromIndex, toIndex - fromIndex);
99 }
100
101 @SuppressWarnings("unchecked")
102 @Override
103 public UnmodifiableListIterator<E> listIterator(int index) {
104
105
106 return (UnmodifiableListIterator<E>)
107 Iterators.forArray(array, offset, size, index);
108 }
109
110
111 }